/*
 *	FileMgr.h is part of Flight_Chess_Portable
 *	Copyright (C) 2008  Poison
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *	Class:			FileMgr
 *	Description:	
 *	Author:			Poison <hbpoison@gmail.com>
 *	Date Created:	2008-02-22
 */

#pragma once

#include <string>

using namespace std;

class IoEntry
{
public:
	enum IoEntryType {
		T_FILE,
		T_DIR
	};
	IoEntry();
	virtual ~IoEntry();
	virtual int open( int mode ) = 0;
	virtual int reopen( int mode ) = 0;
	virtual int close() = 0;
	template <class T> void setPath( T entry_path );
	inline string &getPath() { return *path; }
	inline IoEntryType getType() { return type; }
protected:
	int handler;
	IoEntryType type;
	string * path;
};

class File: public IoEntry
{
public:
	enum FileOpenMode {
		FO_READ = 0x0001,	//Read
		FO_WRITE = 0x0002,	//Write
		FO_APPEND = 0x0100,	//Append
		FO_CREATE = 0x0200,	//Create
		FO_TRUNC = 0x0400	//Trunc
	};
	enum FileSeekMode {
		FS_SET = 0,
		FS_CUR = 1,
		FS_END = 2
	};
	File() { type = T_FILE; }
	template <class T> File( T entry_path, int mode = ( FO_READ | FO_WRITE | FO_CREATE ) );
	virtual ~File();
	int open( int mode = ( FO_READ | FO_WRITE | FO_CREATE ) );
	template <class T> int open( T entry_path, int mode = ( FO_READ | FO_WRITE | FO_CREATE ) );
	int reopen( int mode = ( FO_READ | FO_WRITE | FO_CREATE ) );
	int close();
	int read( void * data, unsigned int size );
	int write( void * date, unsigned int size );
	int seek( int pos, FileSeekMode mode );
	int getSize();
	int getOffset();
};

class Dir: public IoEntry
{
public:
	enum DirOpenMode {
		DO_CREATE = 1,
		DO_EXIST = 0
	};
	Dir() { type = T_DIR; entry_offset = 0; }
	template <class T> Dir( T entry_path, int mode = DO_CREATE );
	virtual ~Dir();
	int open( int mode = DO_CREATE );
	template <class T> int open( T entry_path, int mode = DO_CREATE );
	int reopen( int mode = DO_CREATE );
	int close();
	IoEntry * nextEntry();
	int getEntryCount();
	inline int getEntryOffset() { return entry_offset; }
private:
	int entry_offset;
};
